package cn.baiweigang.uu.service.analyse;

import cn.baiweigang.uu.db.dataobj.StockInfoDo;
import cn.baiweigang.uu.db.repository.AnalyseDayInfoRepository;
import cn.baiweigang.uu.db.repository.HistoryDayInfoRepository;
import cn.baiweigang.uu.enums.StrategyEnum;
import cn.baiweigang.uu.service.dingding.DingService;
import cn.baiweigang.uu.service.mail.MailService;
import cn.baiweigang.uu.service.strategy.Strategy3PService;
import cn.baiweigang.uu.service.strategy.StrategyPMChangeService;
import cn.baiweigang.uu.utils.DateUtil;
import cn.baiweigang.uu.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class AnalyseService {

    @Autowired
    private MailService mailService;

    @Autowired
    private DingService dingService;

    @Autowired
    private Strategy3PService strategy3PService;

    @Autowired
    private StrategyPMChangeService strategyPMChangeService;

    @Autowired
    private AnalyseDayInfoRepository analyseDayInfoRepository;

    @Autowired
    private HistoryDayInfoRepository historyDayInfoRepository;

    //分析指定日期数据
    public void analyse(String ds) {
        //捞取股票列表
        List<StockInfoDo> stockInfoDoList = historyDayInfoRepository.queryByDs(ds);
        if (stockInfoDoList == null || stockInfoDoList.size() < 1) {
            log.info("当天无股票信息  不需要分析");
            return;
        }

        List<StockInfoDo> needSave = new ArrayList<>();

        //目前有2个策略
        //先清空下db  方便刷新数据
        analyseDayInfoRepository.delete(ds, StrategyEnum.MA_3P);
        analyseDayInfoRepository.delete(ds, StrategyEnum.CHANGE_PM);


        for (int i = 0; i < stockInfoDoList.size(); i++) {
            try {
                StockInfoDo stockInfoDo = stockInfoDoList.get(i);
                if (strategy3PService.isHit(stockInfoDo)) {
                    StockInfoDo save = new StockInfoDo();
                    save.setDm(stockInfoDo.getDm());
                    save.setDs(ds);
                    save.setMc(stockInfoDo.getMc());
                    save.setCl(StrategyEnum.MA_3P);
                    save.setPm(stockInfoDo.getPm());
                    needSave.add(save);
                }

                if (strategyPMChangeService.isHit(stockInfoDo)) {
                    StockInfoDo save = new StockInfoDo();
                    save.setDm(stockInfoDo.getDm());
                    save.setDs(ds);
                    save.setMc(stockInfoDo.getMc());
                    save.setCl(StrategyEnum.CHANGE_PM);
                    save.setPm(stockInfoDo.getPm());
                    save.setStrategy(stockInfoDo.getStrategy());
                    needSave.add(save);
                }
            } catch (Throwable e) {
                log.error("选股异常 " + e.getMessage());
            }
        }

        if (needSave.size() > 0) {
            //写db
            analyseDayInfoRepository.insertBatch(needSave);
        }

    }

    public void updatePM(String ds, StrategyEnum strategyEnum) {
        List<StockInfoDo> stockInfoDoList = analyseDayInfoRepository.queryByDsAndCl(ds, strategyEnum);
        for (int i = 0; i < stockInfoDoList.size(); i++) {
            StockInfoDo stockInfoDo = stockInfoDoList.get(i);
            //查排名
            StockInfoDo tmp = historyDayInfoRepository.queryNewestByDm(stockInfoDo.getDm());
            //写入db
            if (tmp != null) {
                analyseDayInfoRepository.updatePm(stockInfoDo.getId(), tmp.getPm());
            }
        }
    }

    public void sendDing(String ds, StrategyEnum strategyEnum) {
        List<StockInfoDo> stockInfoDoList = analyseDayInfoRepository.queryOrderByPm(ds, strategyEnum);
        //发送前10个
        String subject = strategyEnum.getDesc() + "_" + DateUtil.getDs();
        String msg = getDingMsg(stockInfoDoList, 10);
        //发钉钉消息
        dingService.send(subject+"\r\n"+msg);

    }
    private String getDingMsg(List<StockInfoDo> stockInfoDoList, int num) {
        if (stockInfoDoList == null || stockInfoDoList.size() < 1) {
            return "股票列表为空";
        }
        if (stockInfoDoList.size() < num) {
            num = stockInfoDoList.size();
        }
        String tmp = "";
        for (int i = 0; i < num; i++) {
            StockInfoDo stockInfoDo = stockInfoDoList.get(i);
            tmp += stockInfoDo.getDs() + " " + stockInfoDo.getDm() + " " +
                    stockInfoDo.getMc() + " " + stockInfoDo.getPm() + " ";
            if (StringUtil.isNotBlank(stockInfoDo.getStrategy())) {
                tmp += stockInfoDo.getStrategy();
            }
            tmp += "\r\n";
        }
        return tmp;
    }
    public void sendMail(String ds, StrategyEnum strategyEnum) {
        List<StockInfoDo> stockInfoDoList = analyseDayInfoRepository.queryOrderByPm(ds, strategyEnum);
        //发送前10个
        String subject = strategyEnum.getDesc() + "_" + DateUtil.getDs();
        String msg = getMsg(stockInfoDoList, 10);
        //发邮件
        mailService.senMail(subject, msg);
    }

    private String getMsg(List<StockInfoDo> stockInfoDoList, int num) {
        if (stockInfoDoList == null || stockInfoDoList.size() < 1) {
            return "股票列表为空";
        }
        if (stockInfoDoList.size() < num) {
            num = stockInfoDoList.size();
        }
        String tmp = "";
        for (int i = 0; i < num; i++) {
            StockInfoDo stockInfoDo = stockInfoDoList.get(i);
            tmp += stockInfoDo.getDs() + " " + stockInfoDo.getDm() + " " +
                    stockInfoDo.getMc() + " " + stockInfoDo.getPm() + " ";
            if (StringUtil.isNotBlank(stockInfoDo.getStrategy())) {
                tmp += stockInfoDo.getStrategy();
            }
            tmp += "</br>";
        }
        return tmp;
    }
}
